package com.block.job.service;

import cn.hutool.core.util.ObjectUtil;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.*;
import com.block.db.mapper.DataMatchOrderItemMapper;
import com.block.db.mapper.MerchantDataPushJobMapper;
import com.block.match.enums.OrderStatusEnum;
import com.block.match.service.IDataMatchOrderItemService;
import com.block.merchant.model.result.DataPushResult;
import com.block.merchant.service.IMerchantBaseInfoService;
import com.block.merchant.service.IMerchantTaskInfoService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Component
public class DaXiangJinFuRegisterPushJob {

    private static final Logger log = LogFactory.getJobLog();

    @Resource
    private MerchantDataPushJobMapper merchantDataPushJobMapper;
    @Resource
    private DataMatchOrderItemMapper dataMatchOrderItemMapper;

    @Autowired
    NotifyExceptionService notifyExceptionService;
    @Autowired
    IDataMatchOrderItemService dataMatchOrderItemService;
    @Autowired
    IMerchantTaskInfoService merchantTaskInfoService;
    @Autowired
    IMerchantBaseInfoService merchantBaseInfoService;


    /**
     * 大象金服 定时器注册推送
     * */
    @XxlJob("daXiangJinFuXxlJob")
    public ResponseVo query() throws Exception {
        LinkedList<Long> merchantPushJobIds = new LinkedList<>();
        String infoId = XxlJobHelper.getJobParam();
        XxlJobHelper.log("大象金服机构---------接受调度中心参数---------"+infoId);
        List<MerchantDataPushJob> merchantDataPushJobs = merchantDataPushJobMapper.selectPushJobAllByInfoId(Integer.valueOf(infoId));
        if (merchantDataPushJobs.size() == 0){
            XxlJobHelper.log("大象金服机构---------暂无满足条件的数据推送，5分钟后刷新---------");
            return new ResponseVo(AppRspCodeConstant.SUCCESS, "定时任务执行成功");
        }

        XxlJobHelper.log("大象金服机构---------开始执行---------");
        for (MerchantDataPushJob merchantDataPushJob : merchantDataPushJobs) {
            merchantPushJobIds.add(merchantDataPushJob.getId());
            DataMatchOrderItem dataMatchOrderItem = dataMatchOrderItemMapper.getEntityByItemOrderIdLock(String.valueOf(merchantDataPushJob.getOrderItemId()));

            // 订单进行推送
            DataPushResult dataPushResult = merchantBaseInfoService.pushToMerchant(dataMatchOrderItem);

            DataMatchOrderItem updateEntity = new DataMatchOrderItem();
            updateEntity.setId(Long.valueOf(merchantDataPushJob.getOrderItemId()));
            updateEntity.setClickPushFlag(1);

            if (ObjectUtil.isNotEmpty(dataPushResult.getPushOrderId())){
                updateEntity.setPushId(String.valueOf(dataPushResult.getPushOrderId()));
            }

            if (dataPushResult.isPushSuccess()){
                updateEntity.setStatus(OrderStatusEnum.SUCCESS.getValue());
            }else{
                updateEntity.setStatus(OrderStatusEnum.FAILED.getValue());
            }
            dataMatchOrderItemMapper.updateById(updateEntity);
        }
        XxlJobHelper.log("大象金服机构---------数据清理---------结束执行---------");
        merchantDataPushJobMapper.updatePushJobAllByIds(merchantPushJobIds);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, "大象金服机构-定时任务执行成功");

    }
}
